<?php
/**
 * @title 5516. 警告一小时内使用相同员工卡大于等于三次的人 - 分组+剪枝
 * @author start2004
 */

ini_set("memory_limit", "512M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $keyName
     * @param String[] $keyTime
     * @return String[]
     */
    function alertNames($keyName, $keyTime) {
        /**
         * @since 2020-10-03 结果集
         */
        $result = [];

        /**
         * @since 2020-10-03 哈希数组
         */
        $hashs = [];

        /**
         * @since 2020-10-03 长度
         */
        $len = count($keyName);

        /**
         * @since 2020-10-03 时间升序排列
         */
        // array_multisort($keyName, SORT_ASC, SORT_NUMERIC, $keyTime);

        /**
         * @since 2020-10-09 分组
         */
        for ($i=0; $i<$len; $i++){
            $hashs[$keyName[$i]][] = $keyTime[$i];
        }
        // return $hashs;

        /**
         * @since 2020-10-09 迭代
         */
        foreach ($hashs as $name => $times){
            /**
             * @since 2020-10-09 初始化
             */
            $len = count($times);
            if($len >= 3) {
                /**
                 * @since 2020-10-09 排序
                 */
                sort($times);

                /**
                 * @since 2020-10-09 数字化
                 */
                $times[0][2] = 0;
                $times[1][2] = 0;
                // $list = [
                //     $times[0][0] . $times[0][1] . $times[0][3] . $times[0][4],
                //     $times[1][0] . $times[1][1] . $times[1][3] . $times[1][4],
                // ];

                /**
                 * @since 2020-10-09 单个人打卡时间组
                 */
                for ($i = 2; $i < $len; $i++) {
                    /**
                     * @since 2020-10-03 数字化时间
                     */
                    $times[$i][2] = 0;

                    /**
                     * @since 2020-10-09 不能跨天, 第1个元素是否在60分钟内
                     */
                    if ($times[$i] - $times[$i-2] <= 1000) {
                        $result[] = $name;
                        break;
                    } else {
                        /**
                         * @since 2020-10-09 第1个元素不在60分钟内
                         */
                    }
                }
            }
        }

        /**
         * @since 2020-10-09 姓名排序
         */
        sort($result);

        /**
         * @return
         */
        return $result;
    }
}


/**
 * @since 2020-10-09 消耗性能的实例
 * @since 2020-10-09 26*26*26*3 = 2028
 */
$names = $times = [];
for($i=1; $i<=26*26*26*3*2; $i++){
    /**
     * @since 2020-10-09 姓名
     */
    $name = "";
    for($j=1; $j<=3; $j++){
        $name .= "abcdefghijklmnopqrstuvwxyz"[mt_rand(0, 25)];
    }
    $names[] = $name;

    /**
     * @since 2020-10-09 时间
     */
    $hour = mt_rand(0, 23);
    $minute = mt_rand(0, 59);
    $times[] = substr("0". $hour, -2) .":". substr("0". $minute, -2);
}

// echo "[", PHP_EOL;
// echo "[\"", implode('","', $names) ,"\"],", PHP_EOL;
// echo "[\"", implode('","', $times) ,"\"],", PHP_EOL;
// echo "],";
// die();



/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.03_3.php
 */
$datas = [
    [
        $names,
        $times,
    ],
    // [
    //     ["daniel","daniel","daniel","luis","luis","luis","luis"],
    //     ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"],
    // ],
    // [
    //     ["alice","alice","alice","bob","bob","bob","bob"],
    //     ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"]
    // ],
    // [
    //     ["john","john","john"],
    //     ["23:58","23:59","00:01"]
    // ],
    // [
    //     ["leslie","leslie","leslie","clare","clare","clare","clare"],
    //     ["13:00","13:20","14:00","18:00","18:51","19:30","19:49"]
    // ],
    // [
    //     ["clare","clare","clare","clare"],
    //     ["18:00","18:51","19:30","19:49"]
    // ],
    // [
    //     ["bob", "jack", "tom"],
    //     ["09:00", "14:00", "12:00"],
    // ],
    // [
    //     ["a","a","a","a","a","b","b","b","b","b","b"],
    //     ["04:48","23:53","06:36","07:45","12:16","00:52","10:59","17:16","00:36","01:26","22:42"],
    // ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof(2);
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->alertNames(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    // var_dump($result);
    if(count($result)<=20){
        var_dump($result);
    } else {
        var_dump(count($result));
    }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
